package com.wfh.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.wfh.constant.ThumbConstant;
import com.wfh.utils.RedisKeyUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;


/**
 * @Author FengHuan Wang
 * @Date 2025/4/19 18:07
 * @Version 1.0
 */
@Component
@Slf4j
public class SyncThumb2DBCompensatoryJob {


    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private SyncThumb2DBJob syncThumb2DBJob;

    @Scheduled(cron = "0 0 2 * * *")
    public void run(){
        log.info("开始补偿数据");
        Set<String> thumbKeys = redisTemplate.keys(RedisKeyUtil.getTempThumbKey("") + "*");
        Set<String> needHandleDataSet = new HashSet<>();
        thumbKeys.stream().filter(ObjUtil::isNotNull).forEach(thumbKey -> {
            needHandleDataSet.add(thumbKey.replace(ThumbConstant.USER_THUMB_KEY_PREFIX.formatted(""), ""));
        });
        if (CollUtil.isEmpty(needHandleDataSet)){
            log.info("没有要补偿的临时数据");
            return;
        }
        // 补偿数据
        for (String o : needHandleDataSet) {
            syncThumb2DBJob.syncThumb2DBByDate(o);
        }
        log.info("补偿临时数据完成");
    }
}
